#!/bin/sh
#
# Time and date fixer
#
# Visit worldtimeapi.org and change this link according to your location.

## Check root access
[ "$(whoami)" != "root" ] && echo "You must be a root user." && exit 1

## Change this
curl -so /tmp/datetime http://worldtimeapi.org/api/timezone/Europe/Istanbul.txt

LCDATETIME=$(date +%Y%m%d%T)
NWTIME=$(< /tmp/datetime sed -E 's/^.*T//g;3!d;s/\..*//')
NWDATE=$(< /tmp/datetime sed -E 's/T.*//g;3!d;s/^.*\ //;s/-//g')

## if local datetime isn't equal to network date time then change local time to network time
[ "$LCDATETIME" != "${NWDATE}${NWTIME}" ] \
	&& date +%Y%m%d -s "$NWDATE" \
	&& date +%T -s "$NWTIME" \
	&& hwclock -w
